﻿using System.Collections.ObjectModel;
using System.Data;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using PublicDomain.Data.Csv.Metadata;

namespace PublicDomain.UnitTest.Data.Csv.Metadata
{
    [TestClass]
    public class CsvSchemaDataTableTest
    {
        [TestMethod]
        [Description("Tests method CsvSchemaDataTable.GetSchemaTable for the basic case.")]
        public void GetSchemaTableTest()
        {
            var csvDataFormat = new CsvDataFormat
                {
                    CsvColumnsDefinitions = new Collection<CsvColumnDefinition>
                    {
                        new CsvColumnDefinition { ColumnName = "ColumnA", ColumnOrdinal = 0,  DataType = DbType.String, ColumnSize = 30 },
                        new CsvColumnDefinition { ColumnName = "ColumnB", ColumnOrdinal = 1,  DataType = DbType.Double },
                        new CsvColumnDefinition { ColumnName = "ColumnB", ColumnOrdinal = 2,  DataType = DbType.Currency }
                    }
                };

            var actual = CsvSchemaDataTable.GetSchemaTable(csvDataFormat);

            Assert.AreEqual(csvDataFormat.CsvColumnsDefinitions.Count, actual.Rows.Count);
            AssertSchemaTableDataRow(csvDataFormat.CsvColumnsDefinitions[0], actual.Rows[0]);
            AssertSchemaTableDataRow(csvDataFormat.CsvColumnsDefinitions[1], actual.Rows[1]);
            AssertSchemaTableDataRow(csvDataFormat.CsvColumnsDefinitions[2], actual.Rows[2]);
        }

        [TestMethod]
        [Description("Tests method CsvSchemaDataTable.GetSchemaTable for the case when original ColumnsSchema is empty.")]
        public void GetSchemaTableFromEmptyColumnsSchemaTest()
        {
            var csvDataFormat = new CsvDataFormat
            {
            };

            var actual = CsvSchemaDataTable.GetSchemaTable(csvDataFormat);

            Assert.AreEqual(0, csvDataFormat.CsvColumnsDefinitions.Count);
            Assert.AreEqual(csvDataFormat.CsvColumnsDefinitions.Count, actual.Rows.Count);
        }

        private static void AssertSchemaTableDataRow(CsvColumnDefinition expectedCsvColumnSchema, DataRow actualDataRow)
        {
            Assert.AreEqual(expectedCsvColumnSchema.ColumnName, actualDataRow["ColumnName"]);
            Assert.AreEqual(expectedCsvColumnSchema.ColumnOrdinal, actualDataRow["ColumnOrdinal"]);
            Assert.AreEqual(expectedCsvColumnSchema.DataType, (DbType)actualDataRow["DataType"]);
            Assert.AreEqual(expectedCsvColumnSchema.ColumnSize, actualDataRow["ColumnSize"]);
        }
    }
}
